//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Problem.h"
using namespace Cephei::QL::Math::Optimization;
#include <gen/QL/Math/Optimization/Constraint.h>
#include <gen/QL/Math/Optimization/CostFunction.h>
#include <gen/QL/Math/Array.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Optimization::CProblem::CProblem (Cephei::QL::Math::Optimization::ICostFunction^ costFunction, Cephei::QL::Math::Optimization::IConstraint^ constraint, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>^ initialValue) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CCostFunction^ _CcostFunction;
    CConstraint^ _Cconstraint;
    CArray^ _CinitialValue;
    try
    {
#ifdef HANDLE
        _phProblem = NULL;
#endif
        _CcostFunction = safe_cast<CCostFunction^> (costFunction);
        _CcostFunction->Lock();
        QuantLib::CostFunction& _costFunction = static_cast<QuantLib::CostFunction&> (_CcostFunction->GetReference ()); 
        _Cconstraint = safe_cast<CConstraint^> (constraint);
        _Cconstraint->Lock();
        QuantLib::Constraint& _constraint = static_cast<QuantLib::Constraint&> (_Cconstraint->GetReference ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>::IsSome::get (initialValue))
        {
            _CinitialValue = safe_cast<CArray^> (initialValue->Value);
            _CinitialValue->Lock();
        }
        QuantLib::Array& _initialValue = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>::IsSome::get (initialValue) ? static_cast<QuantLib::Array&> (_CinitialValue->GetReference ()) : QuantLib::Array()); //1
        _ppProblem = new boost::shared_ptr<QuantLib::Problem> (new QuantLib::Problem ( _costFunction,  _constraint,  _initialValue ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcostFunction != nullptr) _CcostFunction->Unlock();
        if (_Cconstraint != nullptr) _Cconstraint->Unlock();
        if (_CinitialValue != nullptr) _CinitialValue->Unlock();
    }
}
Cephei::QL::Math::Optimization::CProblem::CProblem (boost::shared_ptr<QuantLib::Problem>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phProblem = NULL;
#endif
	_ppProblem = &childNative;
    
}
Cephei::QL::Math::Optimization::CProblem::CProblem (QuantLib::Problem& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phProblem = NULL;
#endif
	_ppProblem = new boost::shared_ptr<QuantLib::Problem> (&childNative);
    
    _ProblemOwner = owner;
    
}

Cephei::QL::Math::Optimization::CProblem::CProblem (CProblem^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phProblem = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppProblem = new boost::shared_ptr<QuantLib::Problem> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Optimization::CProblem::CProblem (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phProblem = NULL;
#endif
	if (!t->IsSubclassOf(CProblem::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Optimization::CProblem::CProblem (QuantLib::Handle<QuantLib::Problem>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phProblem = &childNative;
	_ppProblem = &static_cast<boost::shared_ptr<QuantLib::Problem>>(childNative.currentLink());
    
    _ProblemOwner = owner;
}
Cephei::QL::Math::Optimization::CProblem::CProblem (QuantLib::Handle<QuantLib::Problem> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phProblem = &childNative;
	_ppProblem = &static_cast<boost::shared_ptr<QuantLib::Problem>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Optimization::CProblem::CProblem (QuantLib::Problem childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phProblem = NULL;
#endif
	_ppProblem = new boost::shared_ptr<QuantLib::Problem> (new QuantLib::Problem (childNative));
    
}
#endif

Cephei::QL::Math::Optimization::CProblem::~CProblem ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppProblem != NULL)
    {
	    delete _ppProblem;
        _ppProblem = NULL;
    }
}
Cephei::QL::Math::Optimization::CProblem::!CProblem ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppProblem != NULL)
    {
	    delete _ppProblem;
    }
}
QuantLib::Problem& Cephei::QL::Math::Optimization::CProblem::GetReference ()
{
    if (_ppProblem == NULL) throw REFNEW NativeNullException ();
	return **_ppProblem;
}
boost::shared_ptr<QuantLib::Problem>& Cephei::QL::Math::Optimization::CProblem::GetShared ()
{
    if (_ppProblem == NULL) throw REFNEW NativeNullException ();
	return *_ppProblem;
}
QuantLib::Problem* Cephei::QL::Math::Optimization::CProblem::GetPointer ()
{
    if (_ppProblem == NULL) throw REFNEW NativeNullException ();
	return &**_ppProblem;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Problem>& Cephei::QL::Math::Optimization::CProblem::GetHandle ()
{
	if (_phProblem == NULL)
	{
		_phProblem = new Handle<QuantLib::Problem> (*_ppProblem);
	}
	return *_phProblem;
}
#endif
bool Cephei::QL::Math::Optimization::CProblem::HasNative () 
{
	return (_ppProblem != NULL);
}

Cephei::QL::Math::Optimization::IConstraint^ Cephei::QL::Math::Optimization::CProblem::Constraint::get ()
{
    try
    {
    	QuantLib::Constraint& _rv = (QuantLib::Constraint&)(*_ppProblem)->constraint ( );   
        Cephei::QL::Math::Optimization::CConstraint^ _nrv = REFNEW Cephei::QL::Math::Optimization::CConstraint (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Optimization::ICostFunction^ Cephei::QL::Math::Optimization::CProblem::CostFunction::get ()
{
    try
    {
    	QuantLib::CostFunction& _rv = (QuantLib::CostFunction&)(*_ppProblem)->costFunction ( );   
        Cephei::QL::Math::Optimization::CCostFunction^ _nrv = REFNEW Cephei::QL::Math::Optimization::CCostFunction (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IArray^ Cephei::QL::Math::Optimization::CProblem::CurrentValue::get ()
{
    try
    {
    	QuantLib::Array& _rv = (QuantLib::Array&)(*_ppProblem)->currentValue ( );   
        Cephei::QL::Math::CArray^ _nrv = REFNEW Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Int32 Cephei::QL::Math::Optimization::CProblem::FunctionEvaluation::get ()
{
    try
    {
    	QuantLib::Integer _rv = (QuantLib::Integer)(*_ppProblem)->functionEvaluation ( );   
        Int32 _nrv = (Int32)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Optimization::CProblem::FunctionValue::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppProblem)->functionValue ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Optimization::IProblem^ Cephei::QL::Math::Optimization::CProblem::Gradient (Cephei::QL::Math::IArray^ grad_f, Cephei::QL::Math::IArray^ x)
{
    CArray^ _Cgrad_f;
    CArray^ _Cx;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cgrad_f = safe_cast<CArray^> (grad_f);
        _Cgrad_f->Lock();
        QuantLib::Array& _grad_f = static_cast<QuantLib::Array&> (_Cgrad_f->GetReference ()); 
        _Cx = safe_cast<CArray^> (x);
        _Cx->Lock();
        QuantLib::Array& _x = static_cast<QuantLib::Array&> (_Cx->GetReference ()); 
    	(*_ppProblem)->gradient ( _grad_f,  _x );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cgrad_f != nullptr) _Cgrad_f->Unlock();
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
Int32 Cephei::QL::Math::Optimization::CProblem::GradientEvaluation::get ()
{
    try
    {
    	QuantLib::Integer _rv = (QuantLib::Integer)(*_ppProblem)->gradientEvaluation ( );   
        Int32 _nrv = (Int32)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Optimization::CProblem::GradientNormValue::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppProblem)->gradientNormValue ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Optimization::IProblem^ Cephei::QL::Math::Optimization::CProblem::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppProblem)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Optimization::IProblem^ Cephei::QL::Math::Optimization::CProblem::SetCurrentValue (Cephei::QL::Math::IArray^ currentValue)
{
    CArray^ _CcurrentValue;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _CcurrentValue = safe_cast<CArray^> (currentValue);
        _CcurrentValue->Lock();
        QuantLib::Array& _currentValue = static_cast<QuantLib::Array&> (_CcurrentValue->GetReference ()); 
    	(*_ppProblem)->setCurrentValue ( _currentValue );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcurrentValue != nullptr) _CcurrentValue->Unlock();
    }
}
Cephei::QL::Math::Optimization::IProblem^ Cephei::QL::Math::Optimization::CProblem::SetFunctionValue (Double functionValue)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Real _functionValue = (QuantLib::Real)ValueHelper::Convert (functionValue); //a
    	(*_ppProblem)->setFunctionValue ( _functionValue );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Optimization::IProblem^ Cephei::QL::Math::Optimization::CProblem::SetGradientNormValue (Double squaredNorm)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Real _squaredNorm = (QuantLib::Real)ValueHelper::Convert (squaredNorm); //a
    	(*_ppProblem)->setGradientNormValue ( _squaredNorm );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Optimization::CProblem::Value (Cephei::QL::Math::IArray^ x)
{
    CArray^ _Cx;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cx = safe_cast<CArray^> (x);
        _Cx->Lock();
        QuantLib::Array& _x = static_cast<QuantLib::Array&> (_Cx->GetReference ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppProblem)->value ( _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
Double Cephei::QL::Math::Optimization::CProblem::ValueAndGradient (Cephei::QL::Math::IArray^ grad_f, Cephei::QL::Math::IArray^ x)
{
    CArray^ _Cgrad_f;
    CArray^ _Cx;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cgrad_f = safe_cast<CArray^> (grad_f);
        _Cgrad_f->Lock();
        QuantLib::Array& _grad_f = static_cast<QuantLib::Array&> (_Cgrad_f->GetReference ()); 
        _Cx = safe_cast<CArray^> (x);
        _Cx->Lock();
        QuantLib::Array& _x = static_cast<QuantLib::Array&> (_Cx->GetReference ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppProblem)->valueAndGradient ( _grad_f,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cgrad_f != nullptr) _Cgrad_f->Unlock();
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Optimization::IProblem^ Cephei::QL::Math::Optimization::CProblem_Factory::Create (Cephei::QL::Math::Optimization::ICostFunction^ costFunction, Cephei::QL::Math::Optimization::IConstraint^ constraint, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>^ initialValue)
{
    return REFNEW CProblem ( costFunction,  constraint,  initialValue);
}
